package org.wmq.javafxmapcollectioncs.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import org.wmq.javafxmapcollectioncs.God;
import org.wmq.javafxmapcollectioncs.model.Entity.MapEntity;

public class UpdateViewController {

    @FXML
    private TextField nameText;

    @FXML
    private TextField mapTypeText;

    @FXML
    private TextField mediumText;

    @FXML
    private TextField urlText;

    @FXML
    private DatePicker publicationTimePicker;

    @FXML
    private DatePicker collectionTimePicker;

    @FXML
    private TextArea introductionText;

    @FXML
    private Button updateButton;

    @FXML
    private void initialize() {
        // 初始化逻辑可以放在这里
        MapEntity mapEntity = God.getGod().getMapEntityList().getCurrentData();
        if (mapEntity == null) {
            return;
        }
        nameText.setText(mapEntity.getName());
        mapTypeText.setText(mapEntity.getMapType());
        mediumText.setText(mapEntity.getMedium());
        urlText.setText(mapEntity.getUrl());
        publicationTimePicker.setValue(mapEntity.getPublicationTime());
        collectionTimePicker.setValue(mapEntity.getCollectionTime());
        introductionText.setText(mapEntity.getIntroduction());
    }

    @FXML
    private void updateMap() {
        // 实现更新地图的逻辑
        System.out.println("Updating the map");
        // 获取输入的值
        String name = nameText.getText();
        String mapType = mapTypeText.getText();
        String medium = mediumText.getText();
        String url = urlText.getText();
        String introduction = introductionText.getText();
        // 获取日期选择器的值
        java.time.LocalDate publicationTime = publicationTimePicker.getValue();
        java.time.LocalDate collectionTime = collectionTimePicker.getValue();

        // 这里可以添加将这些值更新到数据库或模型对象的逻辑
        MapEntity mapEntity = God.getGod().getMapEntityList().getCurrentData();
        if (mapEntity != null) {
            mapEntity.setName(name);
            mapEntity.setUrl(url);
            mapEntity.setIntroduction(introduction);
            mapEntity.setMapType(mapType);
            mapEntity.setMedium(medium);
            mapEntity.setPublicationTime(publicationTime);
            mapEntity.setCollectionTime(collectionTime);
            God.getGod().getMapEntityService().update(mapEntity);
        }
    }
}